﻿using System.Net;
using System.Web;
using System.Web.Routing;
using Rhino.Mocks;

namespace CID.Web.Tests.Controllers
{
    static class StubsBuilder
    {
        public static HttpContextBase BuildHttpContextStub(string targetUrl = "", string httpMethod = "GET")
        {
            var request = MockRepository.GenerateStub<HttpRequestBase>();
            request.Stub(x => x.AppRelativeCurrentExecutionFilePath).Return(targetUrl);
            request.Stub(x => x.HttpMethod).Return(httpMethod);

            var response = MockRepository.GenerateStub<HttpResponseBase>();

            var result = MockRepository.GenerateStub<HttpContextBase>();
            result.Stub(x => x.Request).Return(request);
            result.Stub(x => x.Response).Return(response);
            return result;
        }

        public static RequestContext BuildRequestContextStub(HttpContextBase httpContext = null)
        {
            if (httpContext == null) httpContext = BuildHttpContextStub();

            return new RequestContext(httpContext, new RouteData());
        }

        public static RequestContext BuildAjaxRequestContextStub(HttpContextBase httpContext = null)
        {
            var result = BuildRequestContextStub(httpContext);
            StubRequestAsAjaxRequest(result.HttpContext.Request);
            return result;
        }

        public static void StubRequestAsAjaxRequest(HttpRequestBase request)
        {
            request.Stub(x => x.Headers).Return(new WebHeaderCollection() { { "X-Requested-With", "XMLHttpRequest" } });
        }
    }
}
